package configs

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"log"
	"tao-admin/internal/model"
)

var db *gorm.DB

//var common_db *gorm.DB

func InitDB() {
	var err error
	//初始化gorm
	// db, err := gorm.Open("mysql", "user:password@(localhost)/dbname?charset=utf8mb4&parseTime=True&loc=Local")
	log.Printf("开始连接DB， DriveName：%v, AppSourceName: %v", DBConfig.DriveName, DBConfig.AppSourceName)
	db, err = gorm.Open(DBConfig.DriveName, DBConfig.AppSourceName)
	if err != nil {
		panic(err)
	}
	db.LogMode(DBConfig.ShowSQL)
	//SetMaxIdleCons 设置连接池中的最大连接数
	db.DB().SetMaxIdleConns(DBConfig.MaxIdleConns)
	//设置数据库的最大连接数
	db.DB().SetMaxOpenConns(DBConfig.MaxOpenConns)

	//初始化db
	//common_db, err = gorm.Open(DBConfig.DriveName, DBConfig.CommonDBSourceName)
	//if err !=  nil {
	//	panic(err)
	//}
	//common_db.LogMode(DBConfig.ShowSQL)

	//自动建表
	AutoMigrateTab(db)
}

func AutoMigrateTab(db *gorm.DB) {
	db.AutoMigrate(&model.BillTab{})
}

func CloseDB() {
	db.Close()
}

func GetGormDB() *gorm.DB {
	return db
}
